#include ".\dxmanager.h"

dxManager::dxManager(void)
{
	pD3D = NULL;
 	pd3dDevice = NULL;
}

dxManager::~dxManager(void)
{
}

void dxManager::DisplaySomeText(string texttodisplay, RECT rct)
{
	int someInt;

// Create a colour for the text - in this case blue
D3DCOLOR fontColor = D3DCOLOR_ARGB(255,0,0,255);  


// Draw some text 
someInt = m_font->DrawText(NULL,
						   texttodisplay.c_str(),
						   texttodisplay.length(),
						   &rct,
						   DT_LEFT|DT_WORDBREAK,
						   0xFF000000 );

if(!someInt)
MessageBox(0, "Bad Things Happened", "Bad Things Happened", MB_OK);

//int DrawText( LPD3DXSPRITE pSprite, LPCTSTR pString, INT Count, LPRECT pRect, DWORD Format, D3DCOLOR Color );
//
//pSprite - the first parameter allows a sprite interface to be passed in. Setting this to NULL means Direct3D will use its own internal sprite object to render the text. If you are doing lots of DrawText calls passing your own sprite object can speed up rendering significantly (see font drawing optimisations below).
//pString - this is the string to render. The type of string depends on your projects character set.
//Count - the number of characters in the string. You can set this to the magic number -1 in order to specify that Direct3D should count the characters itself. Note that this only works with null terminated strings.
//pRect - the rectangular area in which the text will be displayed. How the text is justified within this rectangle depends on the next parameter:
//Format - allows you to specify some flags. This allows you to specify the justification of the text within the rectangle plus some other options (see advanced below).
//Color - the final parameter is the colour you want to render the text.

//http://www.toymaker.info/Games/html/text.html

}


bool dxManager::init(HWND hwnd, bool bFullScreen)
{
	if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	{
		lastResult = E_FAIL;
		return false;
	}

	D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );

	// here we use the bool variable that we set earlier
	// if the user wants a fullscreen application then we have to set d3dpp.Windowed to FALSE
	// and we must specify a backbufferformat
	if( bFullScreen )
	{
		d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
		d3dpp.Windowed = FALSE;
	}
	// if this is a windowed application
	// we set d3dpp.Windowed to TRUE and the backbufferformat can be set to  D3DFMT_UNKNOWN
	else
	{
		d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
		d3dpp.Windowed = TRUE;
	}

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferCount  = 1;
	d3dpp.BackBufferHeight = 600;
	d3dpp.BackBufferWidth  = 800;
	d3dpp.hDeviceWindow    = hwnd;

    if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &pd3dDevice ) ) )
    {
		lastResult = E_FAIL;
        return false;
    }

	// Create a D3DX font object
D3DXCreateFont( pd3dDevice,
			   20,
			   0,
			   FW_BOLD,
			   0,
			   FALSE,
			   DEFAULT_CHARSET,
			   OUT_DEFAULT_PRECIS,
			   DEFAULT_QUALITY,
			   DEFAULT_PITCH | FF_DONTCARE,
			   TEXT("Arial"),
			   &m_font );


	return true;
}

void dxManager::shutdown(void)
{
    if( pd3dDevice != NULL) 
	{
        pd3dDevice->Release();
		pd3dDevice = NULL;
	}
    if( pD3D != NULL)
	{
        pD3D->Release();
		pD3D = NULL;
	}

	
	m_font->Release();
}

void dxManager::beginRender()
{
	if( NULL == pd3dDevice )
        return;

    // Clear the backbuffer to a blue color
    pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255,255,255), 1.0f, 0 );
	pd3dDevice->BeginScene();
}

void dxManager::endRender(void)
{
	// Present the backbuffer contents to the display
	pd3dDevice->EndScene();
    pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

IDirect3DSurface9* dxManager::getSurfaceFromBitmap(std::string filename)
{
	HRESULT hResult;
	IDirect3DSurface9* surface = NULL;
	D3DXIMAGE_INFO imageInfo;

	// Get the width and height info from this bitmap
	hResult = D3DXGetImageInfoFromFile(filename.c_str(), &imageInfo);
	if FAILED (hResult)
		return NULL;


	hResult = pd3dDevice->CreateOffscreenPlainSurface(imageInfo.Width, imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
	if (FAILED(hResult))
		return NULL;

	hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, filename.c_str(), NULL, D3DX_DEFAULT, 0, NULL);
	if (FAILED(hResult))
		return NULL;

	return surface;
}

IDirect3DSurface9* dxManager::getBackBuffer(void)
{
	IDirect3DSurface9* backbuffer = NULL;

	if (!pd3dDevice)
		return NULL;

	HRESULT hResult = pd3dDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);
	if (FAILED(hResult))
		return NULL;
	else
		return backbuffer;
}

void dxManager::blitToSurface(IDirect3DSurface9* srcSurface, const RECT *srcRect, const RECT *destRect)
{
	pd3dDevice->StretchRect(srcSurface, srcRect, getBackBuffer(), destRect, D3DTEXF_NONE);
}

void dxManager::onLostDevice()
{
	pd3dDevice->Reset(NULL);

}

void dxManager::onResetDevice()
{
	pd3dDevice->Reset(NULL);

}